import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueMacros from 'unplugin-vue-macros/vite'

//* 组件导入
import importComponents from 'unplugin-vue-components/vite'

//* 自动导入
import autoImport from 'unplugin-auto-import/vite'

//* 资源复制
// import { viteStaticCopy } from 'vite-plugin-static-copy'

//* 解析器: ant-design-vue
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
//* 解析器: vxe-table
import { VxeResolver } from '@vxecli/import-unplugin-vue-components'
//* 解析器: expresso-plus 、Resizer
import { ExResolver, importInclude } from '../packages/expresso-plus/src/scripts'

const baseUrl = fileURLToPath(new URL('.', import.meta.url))

// https://vitejs.dev/config/
export default defineConfig({

  build: {
    outDir: '../dist',
    emptyOutDir: true,
  },

  esbuild: {
    pure: ['console.log'],
    drop: ['debugger'],
  },

  resolve: {
    alias: {
      '@': path.resolve(baseUrl, 'src'),
      '@expresso/plus': path.resolve(baseUrl, '../packages/expresso-plus/src'),
      '$fonts': path.resolve(baseUrl, '../assets/fonts'),
    },
  },

  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
        @import "../../styles/mixin.scss";
        @import "../../styles/font.scss";
        `,
      },
    },
  },

  server: {
    port: 5200,
    cors: true,
    host: '0.0.0.0',
    proxy: {
      '/fjfr/': {
        target: 'http://192.168.1.200',
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/fjfr\//, '/fjfr/'),
      },
      '/hdpu': {
        // target: 'http://1.94.220.228:9898',
        // 王康杰本地
        // target: 'http://192.168.3.8:8000',
        // target: 'http://39.99.187.157:7701',
        target: 'http://192.168.1.250:8077',
        // target: 'http://192.168.1.191:9896',
        // 天卓本地
        // target: 'http://192.168.1.12:8000',
        // target: 'http://192.168.3.12:8000',
        // 张禹本地
        // target: 'http://192.168.3.14:8000',
        changeOrigin: true,
        // rewrite: path => path.replace(/^\/api\//, '/'),
      },
    },
  },

  plugins: [
    VueMacros({
      plugins: {
        vue: vue(),
        vueJsx: vueJsx(),
      },
      betterDefine: false,
    }),

    //# 组件导入
    importComponents({
      resolvers: [
        /* Ant Design Vue */
        AntDesignVueResolver({
          // css in js
          importStyle: false,
        }),

        /* vxe-table */
        // VxeResolver({
        //   importStyle: true,
        // }),
        VxeResolver({
          libraryName: 'vxe-pc-ui',
          // importStyle: true,
        }),
        VxeResolver({
          libraryName: 'vxe-table',
          // importStyle: true,
        }),

        /* expresso-plus */
        ExResolver(),
      ],
      dts: 'src/types/components.d.ts',
      include: importInclude,
    }),

    //# 自动导入
    autoImport({
      dirs: ['../packages/expresso-plus/src'],
      imports: ['vue', 'vue-router', 'pinia'],
      dts: 'src/types/auto-imports.d.ts',
      include: importInclude,
    }),

    //# 资源赋值
    // viteStaticCopy({
    //   targets: [
    //     {
    //       src: normalizePath(path.resolve(baseUrl, '../../assets/diagramDraw')),
    //       dest: '',
    //     },
    //   ],
    // }),
  ],
})
